package edu.sandau.b230111.b23011125.Service.Impl;

import edu.sandau.b230111.b23011125.Global.ProblemListWithProblems;
import edu.sandau.b230111.b23011125.Model.Problem;
import edu.sandau.b230111.b23011125.Model.ProblemList;
import edu.sandau.b230111.b23011125.Model.ProblemListProblem;
import edu.sandau.b230111.b23011125.Repository.ProblemListProblemRepository;
import edu.sandau.b230111.b23011125.Repository.ProblemListRepository;
import edu.sandau.b230111.b23011125.Service.ProblemListService;

import edu.sandau.b230111.b23011125.Service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProblemListServiceImpl implements ProblemListService {
    @Autowired
    private ProblemListRepository problemListRepository;
    @Autowired
    private ProblemListProblemRepository problemListProblemRepository;
    @Autowired
    private ProblemService problemService;

    @Override
    public Page<ProblemList> queryAllProblemList(Integer pageNum, Integer pageSize) {
        Pageable page = PageRequest.of(pageNum,pageSize);
        Page<ProblemList> ans = problemListRepository.findAll(page);
        return ans;
    }

    @Override
    public ProblemList queryProblemListById(String listid) {
        return problemListRepository.getReferenceById(listid);
    }

    @Override
    public ProblemListWithProblems queryProblemListWithProblems(String listid) {
        ProblemList problemList = problemListRepository.getReferenceById(listid);
        ProblemListProblem problemListProblem = problemListProblemRepository.getReferenceById(problemList.getProblemlistid());
        List<Problem> problems = new ArrayList<>();
        problems.add(problemService.queryProblemById(problemListProblem.getProblemAid()));
        problems.add(problemService.queryProblemById(problemListProblem.getProblemBid()));
        problems.add(problemService.queryProblemById(problemListProblem.getProblemCid()));
        problems.add(problemService.queryProblemById(problemListProblem.getProblemDid()));
        problems.add(problemService.queryProblemById(problemListProblem.getProblemEid()));
        ProblemListWithProblems problemListWithProblems= new ProblemListWithProblems(problemList,problems);
        return problemListWithProblems;
    }
}
